﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using virtuaPol.Main;
using virtuaPol.Model;

namespace virtuaPol.Net
{
    /// <summary>
    /// Interaction logic for SaveGameDlg.xaml
    /// </summary>
    public partial class SaveGameDlg : Window
    {

        private CWorld world;
        private CWorld World { get { return world; } set { world = value; } }
        public bool IsCanceled { get; set; }

        private IList<String> games = new List<String>();

        public SaveGameDlg(CWorld world)
        {
            World = world;
            IsCanceled = true;
            if (System.IO.Directory.Exists(App.SAVEGAME_PATH))
            {
                String[] files = System.IO.Directory.GetFiles(App.SAVEGAME_PATH, "*.vpw");
                foreach (String f in files)
                {
                    games.Add(System.IO.Path.GetFileNameWithoutExtension(f));
                }
            }
            InitializeComponent();
            if (games.Count == 0)
            {
                SavedGamesList.IsEnabled = false;
                SavedGamesList.Items.Add("There are no saved games!");
            }
            else SavedGamesList.ItemsSource = games;
        }

        private void BtSave_Click(object sender, RoutedEventArgs e)
        {
            // Check for name
            if (TxtSaveName.Text == "")
            {
                MessageBox.Show("You have to enter a name first!");
                return;
            }
            
            // Check for invalid chars
            if (TxtSaveName.Text.Contains("\\")
                || TxtSaveName.Text.Contains("."))
            {
                MessageBox.Show("The name you entered contains invalid characters!");
                return;
            }

            // Check whether file already exists
            if (System.IO.File.Exists(App.SAVEGAME_PATH + TxtSaveName.Text + App.WORLD_FILE_EXTENSION))
            {
                MessageBoxResult res = MessageBox.Show("Are you shure that you want to overwrite the savegame ''?",
                    "Save Game", MessageBoxButton.YesNo, MessageBoxImage.Question);
                if (res == MessageBoxResult.No)
                    return;
                // Delete existing file
                System.IO.File.Delete(App.SAVEGAME_PATH + TxtSaveName.Text + App.WORLD_FILE_EXTENSION);
            }

            // Check if savegame folder exists
            if (!System.IO.Directory.Exists(App.SAVEGAME_PATH))
                System.IO.Directory.CreateDirectory(App.SAVEGAME_PATH);

            WorldLoader.save(ref world, App.SAVEGAME_PATH + TxtSaveName.Text + App.WORLD_FILE_EXTENSION);

            IsCanceled = false;
            this.Close();
        }

        private void BtCancel_Click(object sender, RoutedEventArgs e)
        {
            IsCanceled = true;
            this.Close();
        }

        private void SavedGamesList_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            TxtSaveName.Text = (string)SavedGamesList.SelectedItem;
        }
    }
}
